package com.aiguigu.lottery.prize.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableRabbit
public class RabbitMQConfig {

    // 中奖消息相关常量
    public static final String PRIZE_WIN_QUEUE = "prize.win.queue";
    public static final String PRIZE_WIN_EXCHANGE = "prize.win.exchange";
    public static final String PRIZE_WIN_ROUTING_KEY = "prize.win.routing.key";
    

    public static final String LOTTERY_QUEUE = "lottery.queue";
    public static final String LOTTERY_EXCHANGE = "lottery.exchange";
    public static final String LOTTERY_WIN_ROUTING_KEY = "lottery.win";

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory,
                                                                             MessageConverter messageConverter) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter);
        
        // 设置并发消费者数量
        factory.setConcurrentConsumers(1);
        factory.setMaxConcurrentConsumers(3);
        
        // 设置消息确认模式
        factory.setAcknowledgeMode(AcknowledgeMode.AUTO);
        
        // 设置预取数量
        factory.setPrefetchCount(1);
        
        // 设置消息重试
        factory.setDefaultRequeueRejected(false);
        
        return factory;
    }

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, MessageConverter messageConverter) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter);
        
        // 设置消息确认回调
        rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
            if (!ack) {
                System.err.println("消息发送失败：" + cause);
            }
        });
        
        // 设置消息返回回调
        rabbitTemplate.setReturnsCallback(returned -> {
            System.err.println("消息被退回：" + returned.getMessage());
        });
        
        // 设置强制消息投递
        rabbitTemplate.setMandatory(true);
        
        return rabbitTemplate;
    }
    
    // 中奖消息相关配置
    @Bean
    public Queue prizeWinQueue() {
        return QueueBuilder.durable(PRIZE_WIN_QUEUE).build();
    }
    
    @Bean
    public TopicExchange prizeWinExchange() {
        return ExchangeBuilder.topicExchange(PRIZE_WIN_EXCHANGE)
                .durable(true)
                .build();
    }
    
    @Bean
    public Binding prizeWinBinding() {
        return BindingBuilder.bind(prizeWinQueue())
                .to(prizeWinExchange())
                .with(PRIZE_WIN_ROUTING_KEY);
    }
    
    @Bean
    public Queue lotteryQueue() {
        return QueueBuilder.durable(LOTTERY_QUEUE).build();
    }

    @Bean
    public TopicExchange lotteryExchange() {
        return ExchangeBuilder.topicExchange(LOTTERY_EXCHANGE)
                .durable(true)
                .build();
    }

    @Bean
    public Binding lotteryBinding() {
        return BindingBuilder.bind(lotteryQueue())
                .to(lotteryExchange())
                .with("lottery.#");
    }
}